<template>
   
  <!-- 自定义对话框 -->
  <custom-dialog
    ref="customDialogRef"
    v-slot="{ formData, readonly, mode }"
    :api-map="{update: engineeringContractUpdateActualTime}"
    header="*实际时间"
    @request-success="handleDialogRequestSuccess"
  >
    <t-form-item
      label="实际开工时间"
      name="actualBeginTime"
      :rules="[
        {
          validator: () => {
            if (!formData.actualEndTime) return true;
            return dayjs(formData.actualBeginTime).isBefore(formData.actualEndTime);
          },
          message: '实际开工时间不能大于实际完工时间',
        },
      ]"
    >
      <t-date-picker 
        allow-input
        v-model:value="formData.actualBeginTime"
        clearable
        placeholder="请选择实际开工时间"
        value-type="time-stamp"
        :readonly="readonly"
        style="width: 100%"
        @change="
          (value) => {
            if (value) {
              value = dayjs(value).format('YYYY-MM-DD 00:00:00');
              value = dayjs(value).valueOf();
            }
            formData.actualBeginTime = value ? value : undefined;
          }
        "
      />
    </t-form-item>
    <t-form-item
      label="实际完工时间"
      name="actualEndTime"
      :rules="[
        {
          validator: () => {
            if (!formData.actualBeginTime) return true;
            return dayjs(formData.actualEndTime).isAfter(formData.actualBeginTime);
          },
          message: '实际完工时间不能小于实际开工时间',
        },
      ]"
    >
      <t-date-picker 
        allow-input
        v-model:value="formData.actualEndTime"
        clearable
        placeholder="请选择实际完工时间"
        value-type="time-stamp"
        :readonly="readonly"
        style="width: 100%"
        @change="
          (value) => {
            if (value) {
              value = dayjs(value).format('YYYY-MM-DD 23:59:59');
              value = dayjs(value).valueOf();
            }
            formData.actualEndTime = value ? value : undefined;
          }
        "
      />
    </t-form-item>
   
  </custom-dialog>
</template>

<script setup lang="ts">
import dayjs from 'dayjs';
import { ref, useTemplateRef, watch } from 'vue';
import { engineeringContractUpdateActualTime } from '@/api/project/details'
import CustomDialog from '@/components/custom/dialog/index.vue';
// emits
const emits = defineEmits(['requestSuccess']);
// 自定义对话框ref
const customDialogRef = useTemplateRef('customDialogRef');

// 处理打开对话框
const handleOpenDialog = (mode: 'create' | 'read' | 'update', params: Record<string, any>) => {
  customDialogRef.value.open(mode, params);
};

// 处理对话框请求成功
const handleDialogRequestSuccess = (result: Record<string, any>) => {
  emits('requestSuccess', result);
};


defineExpose({
  open: handleOpenDialog,
});
</script>

<style lang="less" scoped>
.text-only-cascader {
  :deep(.t-input__suffix-icon) {
    display: none;
  }
  
}
</style>
